package com.qin.EducationalManger.student.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qin.EducationalManger.course.model.CourseModel;
import com.qin.EducationalManger.student.model.StudentModel;
import com.qin.EducationalManger.utils.JdbcUtils;


public class StudentDao {
	JdbcUtils jdbcUtils = new JdbcUtils();

	public static int insert(StudentModel model) {

		String sql = "insert into student (student_id,student_name,age,student_sex,course_id) values(?,?,?,?,?)";
		List<Object> vals = Arrays.asList(model.getStudentId(), model.getStudentName(), model.getAge(),
				model.getStudentSex(), model.getCourseId());

		return JdbcUtils.update(sql, vals);

	}

	public static int delet(StudentModel mode) {
		String sql = "delete from student where student_id like ?";
		List<Object> vals = Arrays.asList(mode.getStudentId());

		
		
		return JdbcUtils.update(sql, vals);

	}
	
	//按照条件修改

	public static int update(StudentModel mode) {

		StringBuffer sql = new StringBuffer("update student set student_id ="+mode.getStudentId()); // 储存sql语句
		List<Object> vals = new ArrayList<Object>(); // 用于存储 要修改的 内容
       
		String studentName = mode.getStudentName(); // 判断是否要对该值做出修改
		if (studentName != null && studentName.intern().isEmpty() == false) {
			sql.append(" ,student_name = ?");
			vals.add(studentName);
		}

		Integer age = mode.getAge();
		if (age != null) {
			sql.append(",age = ?");
			vals.add(age);

		}
		String studentSex = mode.getStudentSex();
		if (studentSex != null && studentSex.intern().isEmpty() == false) {
			sql.append(",student_sex = ?");
			vals.add(studentSex);
		}


		String courseId = mode.getCourseId();
		if(courseId!=null&&courseId.intern().isEmpty()==false) {
			sql.append(",course_id = ?");
			vals.add(courseId);
		}
		sql.append(" where student_id = " + mode.getStudentId());

		return JdbcUtils.update(sql.toString(), vals);

	}
	
	//按照条件查询
	public  static List<StudentModel> select(StudentModel model) {
	StringBuffer sql = new StringBuffer("select * from student where 1=1 ");
	
	Map<String, String> fields = new HashMap<String, String>();
	fields.put("studentId", "student_id");
	fields.put("studentName", "student_name");
	fields.put("age", "age");
	fields.put("studentSex", "student_sex");

	fields.put("courseId", "course_id");

	
	
	String courseName =  model.getStudentId();
	
	List<Object> vals = appendWhere(sql, model);
	
	
	return  JdbcUtils.Query(sql.toString(), vals, StudentModel.class, fields);
	 
	

	 
	
	
	
     
		
	}
	
	private static List<Object>  appendWhere(StringBuffer sql, StudentModel model) {
		
		List<Object> vals = new ArrayList<Object>();
		String studentName = model.getStudentName();
		if(studentName!=null&&studentName.intern().isEmpty()==false) {
			sql.append(" and student_name like ?");
			vals.add(studentName);
		}
		Integer age =model.getAge();
		if(age!=null) {
			sql.append(" and age=?");
			vals.add(age);
			
		}
		String studentId = model.getStudentId();
		if(studentId!=null&&studentId.intern().isEmpty()==false) {
			sql.append(" and student_id=?");
			vals.add(studentId);	
		}
		String studentSex = model.getStudentSex();
		if(studentSex!=null &&studentSex.intern().isEmpty()==false) {
			sql.append(" and student_sex=?");
			vals.add(studentSex);
		}
		
		

		
		String courseId = model.getCourseId();
		if(courseId!=null&&courseId.intern().isEmpty()==false) {
			
			sql.append(" and course_id =?");
			vals.add(courseId);
		}
		
		if(model.isPageOn()) {
			sql.append("limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
			
		}
		
		return vals;
		
		
	}
	// 查询信息总条数
	public static Integer  selectCount(StudentModel model) {
		String studentId = model.getStudentId();
		String studentName = model.getStudentName();
		List <Object> vals = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select count(student_id) as count from student where 1=1");
		if(studentId!=null&&studentId.intern().isEmpty()==false) {
			sql.append(" and student_id like ?");
			vals.add(studentId);
		}
		
		
		if(studentName!=null&&studentName.intern().isEmpty()==false) {
			sql.append(" and student_name like ?");
			vals.add(studentName);
		}
	
	
		 
		
		return JdbcUtils.selectCount(sql.toString(),vals);
		
	}
  
}
